<?php
namespace app\admin\controller;

use app\common\cache\common\IAreaCache;
use app\common\controller\AdminBaseController;
use app\common\enum\IsLeader;
use app\common\enum\IsDelete;
use app\common\ext\IDb;
use app\common\ext\IRequest;
use think\Db;

class CooperateAdmin extends AdminBaseController{
    /**
     * @功能：合作商管理员列表
     * @开发者：wdd
     * @return string
     */
    public function cooperate_admin_list(){
        //接收参数
        $cooperate_admin_id = IRequest::get("cooperate_admin_id");
        $cooperate_id = IRequest::get("cooperate_id");
        $account = IRequest::get("account");
        $real = IRequest::get("real");
        $nick = IRequest::get("nick");
        $phone = IRequest::get("phone");
        $is_leader = IRequest::get("is_leader");

        $cooperate_admin_where = null;
        if(!empty($cooperate_admin_id)) $cooperate_admin_where['cooperate_admin_id'] = $cooperate_admin_id;
        if(!empty($cooperate_id)) $cooperate_admin_where['cooperate_id'] = $cooperate_id;
        if(!empty($account)) $cooperate_admin_where['account'] = array("like","%{$account}%");
        if(!empty($real)) $cooperate_admin_where['real'] = array("like","%{$real}%");
        if(!empty($nick)) $cooperate_admin_where['nick'] = array("like","%{$nick}%");
        if(!empty($phone)) $cooperate_admin_where['phone'] = array("like","%{$phone}%");
        if(!empty($is_leader)) $cooperate_admin_where['is_leader'] = $is_leader;
        $cooperate_admin_list = IDb::getInstance('cooperate_admin')->setDbWhere($cooperate_admin_where)->setDbOrder('cooperate_admin_id desc')->pag();
        if ($cooperate_admin_list === false){
            error('获取业务员列表失败');
        }

        //页面赋值
        $this->assign("cooperate_admin_id",$cooperate_admin_id);
        $this->assign("cooperate_id",$cooperate_id);
        $this->assign("account",$account);
        $this->assign("real",$real);
        $this->assign("nick",$nick);
        $this->assign("phone",$phone);
        $this->assign("is_leader",$is_leader);
        $this->assign("cooperate_admin_list",$cooperate_admin_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：贷经理详情
     * @开发者：wdd
     * @return string
     */
    public function cooperate_admin_info(){
        //接收参数
        $cooperate_admin_id = IRequest::get("cooperate_admin_id",IRequest::NOT_EMPTY,"信贷经理编号不能为空！");

        //判断名称是否已经存在
        $where['cooperate_admin_id'] = $cooperate_admin_id;
        $cooperate_admin_info = IDb::getInstance('cooperate_admin')->setDbWhere($where)->row();
        if(empty($cooperate_admin_info)){
            error("取得信贷经理详情失败！");
        }

        //设置页码layout
        $this->layout("layout_empty");

        //页面赋值
        $this->assign("cooperate_admin_info",$cooperate_admin_info);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：添加管理员
     * @开发者：wdd
     * @return string
     */
    public function cooperate_admin_add(){
        $cooperate_list = IDb::getInstance('cooperate')->setDbFiled('cooperate_id,cooperate_name')->sel();
        if ($cooperate_list === false){
            error('获取合作商失败');
        }

        //页面赋值
        $this->assign("cooperate_list",$cooperate_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：编写管理员
     * @开发者：wdd
     * @return string
     */
    public function cooperate_admin_edit(){
        //接收参数
        $cooperate_admin_id = IRequest::get("cooperate_admin_id",IRequest::NOT_EMPTY,"信贷经理编号不能为空！");

        $where['cooperate_admin_id'] = $cooperate_admin_id;
        $cooperate_admin_info = IDb::getInstance('cooperate_admin')->setDbWhere($where)->row();
        if ($cooperate_admin_info === false){
            error('获取管理员详情失败');
        }

        $cooperate_list = IDb::getInstance('cooperate')->setDbFiled('cooperate_id,cooperate_name')->sel();
        if ($cooperate_list === false){
            error('获取合作商失败');
        }

        //页面赋值
        $this->assign("cooperate_list",$cooperate_list);
        $this->assign("cooperate_admin_info",$cooperate_admin_info);
        $this->assign("cooperate_admin_id",$cooperate_admin_id);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：编辑管理员提交
     * @开发者：wdd
     * @return string
     */
    public function cooperate_admin_submit(){
        //接收参数
        $cooperate_admin_id = IRequest::get("cooperate_admin_id");
        $cooperate_id = IRequest::get("cooperate_id",IRequest::NOT_EMPTY,"合作商不能为空！");
        $phone = IRequest::get("phone",IRequest::MOBILE,"管理员手机号码错误！");
        $nick = IRequest::get("nick");
        $real = IRequest::get("real");
        $avatar = IRequest::get("avatar");
        $identity = IRequest::get("identity");
        $remark = IRequest::get("remark");

        //判断名称是否重复
        $cooperate_admin_where = null;
        $cooperate_admin_where['phone'] = $phone;
        $cooperate_admin_info = IDb::getInstance('cooperate_admin')->setDbWhere($cooperate_admin_where)->row();
        if ($cooperate_admin_info === false) {

            //提示错误信息
            error("获取管理员详情失败");
        }

        if (empty($cooperate_admin_id)){

            //判断账号是否已被使用
            if (!empty($cooperate_admin_info)){
                error('手机号已被使用');
            }

            //添加代理商负责人
            $cooperate_admin_data = null;
            $cooperate_admin_data['cooperate_id'] = $cooperate_id;
            $cooperate_admin_data['pwd'] = md5(md5('88888888'));
            $cooperate_admin_data['phone'] = $phone;
            $cooperate_admin_data['nick'] = $nick;
            $cooperate_admin_data['real'] = $real;
            $cooperate_admin_data['avatar'] = $avatar;
            $cooperate_admin_data['identity'] = $identity;
            $cooperate_admin_data['remark'] = $remark;
            $cooperate_admin_data['is_leader'] = IsLeader::No;
            $cooperate_admin_data['reg_ip'] = get_client_ip();
            $cooperate_admin_data['login_count'] = 0;
            $cooperate_admin_data['reg_time'] = get_date_time();
            $cooperate_admin_add = IDb::getInstance('cooperate_admin')->setDbData($cooperate_admin_data)->add();
            if ($cooperate_admin_add === false){
                error('添加管理员失败');
            }
        }else{
            //判断账号是否已被使用
            if (!empty($cooperate_admin_info) && $cooperate_admin_info['cooperate_admin_id'] != intval($cooperate_admin_id)){
                error('手机号已被使用');
            }

            //添加代理商负责人
            $cooperate_admin_data = null;
            $cooperate_admin_where = null;
            $cooperate_admin_where['cooperate_admin_id'] = $cooperate_admin_id;
            $cooperate_admin_data['cooperate_id'] = $cooperate_id;
            $cooperate_admin_data['phone'] = $phone;
            $cooperate_admin_data['nick'] = $nick;
            $cooperate_admin_data['real'] = $real;
            $cooperate_admin_data['avatar'] = $avatar;
            $cooperate_admin_data['identity'] = $identity;
            $cooperate_admin_data['remark'] = $remark;
            $cooperate_admin_upd = IDb::getInstance('cooperate_admin')->setDbWhere($cooperate_admin_where)->setDbData($cooperate_admin_data)->upd();
            if ($cooperate_admin_upd === false){
                error('编辑管理员失败');
            }
        }

        //画面跳转
        $this->success("操作成功！",Url("cooperate_admin_list"));
    }

    /**
     * @功能：禁用合作商
     * @开发者：wdd
     * @return string
     */
    public function cooperate_admin_del(){
        //接收参数
        $cooperate_admin_id = IRequest::get("cooperate_admin_id",IRequest::NOT_EMPTY,"信贷公司编号不能为空！");

        //修改管理员信息
        $cooperate_admin_where['cooperate_admin_id'] = $cooperate_admin_id;
        $cooperate_admin_data['is_delete'] = IsDelete::Yes;
        $cooperate_admin_upd = IDb::getInstance('cooperate_admin')->setDbWhere($cooperate_admin_where)->setDbData($cooperate_admin_data)->upd();
        if($cooperate_admin_upd === false){

            //错误信息
            error("管理员禁用失败！");
        }

        //画面跳转
        $this->success("操作成功！");
    }

    /**
     * @功能：还原合作商
     * @开发者：wdd
     * @return string
     */
    public function cooperate_admin_restore(){
        //接收参数
        $cooperate_admin_id = IRequest::get("cooperate_admin_id",IRequest::NOT_EMPTY,"信贷公司编号不能为空！");

        //修改管理员信息
        $cooperate_admin_where['cooperate_admin_id'] = $cooperate_admin_id;
        $cooperate_admin_data['is_delete'] = IsDelete::No;
        $cooperate_admin_upd = IDb::getInstance('cooperate_admin')->setDbWhere($cooperate_admin_where)->setDbData($cooperate_admin_data)->upd();
        if($cooperate_admin_upd === false){

            //错误信息
            error("管理员禁用失败！");
        }

        //画面跳转
        $this->success("操作成功！");
    }
//    /**
//     * @功能：合作商实名认证
//     * @开发者：wdd
//     * @return string
//     */
//    public function cooperate_admin_real(){
//        //接收参数
//        $cooperate_admin_id = IRequest::get("cooperate_admin_id",IRequest::NOT_EMPTY,"信贷公司编号不能为空！");
//
//        //判断名称是否已经存在
//        $where['cooperate_admin_id'] = $cooperate_admin_id;
//        $cooperate_admin_info = IDb::getInstance('cooperate_admin')->setDbWhere($where)->row();
//        if(empty($cooperate_admin_info)){
//            error("取得信贷经理详情失败！");
//        }
//
//        //页面赋值
//        $this->assign("cooperate_admin_info",$cooperate_admin_info);
//        $this->assign("cooperate_admin_id",$cooperate_admin_id);
//
//        //渲染模板输出
//        return view();
//    }
//
//    /**
//     * @功能：合作商实名认证提交
//     * @开发者：wdd
//     * @return string
//     */
//    public function cooperate_admin_real_submit(){
//        //接收参数
//        $cooperate_admin_id = IRequest::get("cooperate_admin_id",IRequest::NOT_EMPTY,"信贷公司编号不能为空！");
//        $is_real = IRequest::get("is_real",IRequest::NOT_EMPTY,"认证状态不能为空！");
//
//        $where['cooperate_admin_id'] = $cooperate_admin_id;
//        $data['is_real'] = $is_real;
//        $cooperate_admin_real = IDb::getInstance('cooperate_admin')->setDbData($data)->setDbWhere($where)->upd();
//        if ($cooperate_admin_real === false){
//            error('认证失败');
//        }
//
//        //画面跳转
//        $this->success("操作成功！",Url("cooperate_admin_list"));
//    }
}